import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
import { Table } from 'antd';
import React, { forwardRef, useImperativeHandle, useMemo } from 'react';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import DraggableBodyRow from './DraggableBodyRow';
import useColumns from './hooks/useColumns';
import useTable from './hooks/useTable';
import useValue from './hooks/useValue';
import TopStoreInput from './TopStoreInput';
var _excluded = [
  'name',
  'extraFieldNamesForSubmit',
  'initialValue',
  'columns',
  'usingCustomActionColumn',
  'value',
  'form',
  'uniqueKey',
  'rowExpandable',
  'dragable',
  'addable',
  'min',
  'max',
  'rowExpandMin',
  'rowExpandMax',
  'rowExpandAutoAdd',
  'rowKey',
  'validateRow',
  'Fields',
  'rules',
];
/**
 * 可编辑表格
 */
function EditableTable(_ref, ref) {
  var name = _ref.name,
    extraFieldNamesForSubmit = _ref.extraFieldNamesForSubmit,
    _ref$initialValue = _ref.initialValue,
    initialValue =
      _ref$initialValue === void 0
        ? [
            {
              _vid: 0,
            },
          ]
        : _ref$initialValue,
    columnsProp = _ref.columns,
    usingCustomActionColumn = _ref.usingCustomActionColumn,
    value = _ref.value,
    form = _ref.form,
    _ref$uniqueKey = _ref.uniqueKey,
    uniqueKey = _ref$uniqueKey === void 0 ? 'id' : _ref$uniqueKey,
    rowExpandable = _ref.rowExpandable,
    dragable = _ref.dragable,
    _ref$addable = _ref.addable,
    addable = _ref$addable === void 0 ? true : _ref$addable,
    min = _ref.min,
    max = _ref.max,
    rowExpandMin = _ref.rowExpandMin,
    rowExpandMax = _ref.rowExpandMax,
    rowExpandAutoAdd = _ref.rowExpandAutoAdd,
    rowKeyProp = _ref.rowKey,
    validateRow = _ref.validateRow,
    Fields = _ref.Fields,
    rules = _ref.rules,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useTable = useTable({
      value: value,
      uniqueKey: uniqueKey,
      rowKey: rowKeyProp,
    }),
    dataSource = _useTable.dataSource,
    rowKey = _useTable.rowKey,
    expandedKeys = _useTable.expandedKeys,
    onExpand = _useTable.onExpand,
    expandRow = _useTable.expandRow;
  var _useValue = useValue(form, {
      name: name,
      initialValue: initialValue,
      value: value,
      rowKey: rowKey,
      min: min,
      max: max,
      rowExpandMin: rowExpandMin,
      rowExpandMax: rowExpandMax,
      expandRow: expandRow,
    }),
    addItem = _useValue.addItem,
    removeItem = _useValue.removeItem,
    moveItem = _useValue.moveItem,
    setParentValue = _useValue.setParentValue,
    setChildrenValue = _useValue.setChildrenValue,
    getValue = _useValue.getValue,
    setValue = _useValue.setValue;
  var _useColumns = useColumns(form, {
      columns: columnsProp,
      usingCustomActionColumn: usingCustomActionColumn,
      name: name,
      uniqueKey: uniqueKey,
      extraFieldNamesForSubmit: extraFieldNamesForSubmit,
      rowExpandable: rowExpandable,
      addable: addable,
      addItem: addItem,
      removeItem: removeItem,
      setParentValue: setParentValue,
      setChildrenValue: setChildrenValue,
      rowExpandAutoAdd: rowExpandAutoAdd,
      Fields: Fields,
    }),
    columns = _useColumns.columns;
  useImperativeHandle(
    ref,
    function () {
      return {
        addItem: addItem,
        removeItem: removeItem,
        getValue: getValue,
        setValue: setValue,
      };
    },
    [addItem, removeItem, getValue, setValue],
  );
  var restProps = useMemo(
    function () {
      return {
        rowKey: rowKey,
        expandable: {
          expandedRowKeys: expandedKeys,
        },
        onExpand: onExpand,
        footer: function footer() {
          return /*#__PURE__*/ React.createElement(TopStoreInput, {
            name: name,
            columns: columnsProp,
            rules: rules,
            validateRow: validateRow,
          });
        },
      };
    },
    [rowKey, expandedKeys, onExpand, name, columnsProp, validateRow, rest.pagination],
  );
  return /*#__PURE__*/ React.createElement(
    'div',
    {
      className: 'szzj-antd-form-editable-table',
    },
    dragable
      ? /*#__PURE__*/ React.createElement(
          DndProvider,
          {
            backend: HTML5Backend,
          },
          /*#__PURE__*/ React.createElement(
            Table,
            _objectSpread(
              _objectSpread(
                _objectSpread(
                  {
                    size: 'small',
                  },
                  rest,
                ),
                {},
                {
                  columns: columns,
                  dataSource: dataSource,
                },
                restProps,
              ),
              {},
              {
                components: {
                  body: {
                    row: DraggableBodyRow,
                  },
                },
                // @ts-ignore
                onRow: function onRow(row, index) {
                  return {
                    index: index,
                    moveRow: function moveRow(dragIndex, hoverIndex) {
                      return moveItem(row, dragIndex, hoverIndex);
                    },
                  };
                },
              },
            ),
          ),
        )
      : /*#__PURE__*/ React.createElement(
          Table,
          _objectSpread(
            _objectSpread(
              {
                size: 'small',
              },
              rest,
            ),
            {},
            {
              columns: columns,
              dataSource: dataSource,
            },
            restProps,
          ),
        ),
  );
}
export default /*#__PURE__*/ forwardRef(EditableTable);
